
package GUI;

import domain.DomainController;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
/**
 * This class represents the HTTPDragon tray icon.
 * @author HTTPDragon Team
 */
public class DragonTray implements Observer{
   private DomainController dc;
   private TrayIcon trayIcon;
   private SystemTray tray = SystemTray.getSystemTray();
   private PopupMenu popup = new PopupMenu();
   private static final MenuItem EXITPROGITEM = new MenuItem("Exit");
   private static final MenuItem STARTITEM = new MenuItem("Start");
   private static final MenuItem STOPITEM = new MenuItem("Stop");
   private static final MenuItem RESTARTITEM = new MenuItem("Restart");
   private Image dragonAwake = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/GUI/resources/Dragon.png"));
   private Image dragonAsleep = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/GUI/resources/DragonStopped.png"));
   private boolean traySupported;
 /**
  * Public constructor for creating a DragonTray. 
  * @param dc DomainController
  */
  public DragonTray(final DomainController dc){
      this.dc = dc;
      dc.addObserver(this);
      traySupported=SystemTray.isSupported();
        if (traySupported) {
            ActionListener exitListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            };
            ActionListener startActionListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {                    
                      dc.serverStart();
                }
            };
            ActionListener stopActionListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {                  
                      dc.serverStop();     
                }
            };
            ActionListener restartActionListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {                  
                      dc.serverRestart();             
                }
            };            
            popup.add(STARTITEM);
            STARTITEM.addActionListener(startActionListener);            
            popup.add(STOPITEM);
            STOPITEM.addActionListener(stopActionListener); 
            popup.add(RESTARTITEM);
            RESTARTITEM.addActionListener(restartActionListener); 
            
            popup.add(EXITPROGITEM);
            EXITPROGITEM.addActionListener(exitListener);

            trayIcon = new TrayIcon(dragonAwake, "HTTPDragon", popup);


            trayIcon.setImageAutoSize(true);

            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                System.err.println("Tray Icon could not be added.");
            }  
//           reloadInfo();
        }else{
             //  System Tray is not supported, don't do anything
        }
    }
  /**
   * Displays a message in the desktop environment.
   * @param title The message's title.
   * @param message A message to show to the user.
   */
  public void infoMessage(String title, String message){
    if(traySupported){
        trayIcon.displayMessage(title, message, TrayIcon.MessageType.INFO);
    }
  }
  private void updateMenu(){
            if(dc.serverStatusBoolean()){ 
                trayIcon.setImage(dragonAwake);
                STARTITEM.setEnabled(false);         
                STOPITEM.setEnabled(true);           
                RESTARTITEM.setEnabled(true);  
            }else{
                trayIcon.setImage(dragonAsleep);
                STARTITEM.setEnabled(true);        
                STOPITEM.setEnabled(false);      
                RESTARTITEM.setEnabled(false);        
            }
  }
     /**
     * Updates the current server status and displays it in a tool tip 
     */
  private void reloadInfo(){  
      if(traySupported){
              updateMenu();          
              infoMessage("HTTPDragon", "Health: "+dc.serverStatus());   
              trayIcon.setToolTip("HTTPDragon Health: " + dc.serverStatus());   
      }
  }
     /**
     * This method updates the Tray when the server(s) changed state 
     * @param o DomainController
     * @param arg Arguments to pass
     */
    @Override
    public void update(Observable o, Object arg) {
            reloadInfo();
    }
}
